#!/bin/sh

hostname Accelerator

# Populate the serial number file.
SERIAL=$(hnvram -br 1st_serial_number)
[ -n "$SERIAL" ] &&
echo "$SERIAL" >/etc/serial &&
chmod 444 /etc/serial

GPN=$(hnvram -qr GPN)
[ -n "$GPN" ] &&
echo "$GPN" >/tmp/gpn &&
chmod 444 /tmp/gpn

# Do this before gpio-mailbox
update-gpio-links

# Start the gpio mailbox ASAP, since it blinks the LEDs to show boot progress.
echo "Starting gpio-mailbox"
mkdir -p /tmp/gpio/ledcontrol
leds x2 3 0  # blink activity while booting (same as LINKDOWN)
echo 30 >/tmp/gpio/fanpercent  # low fan at first
if is-secure-boot; then
  : >/tmp/gpio/ledcontrol/secure_boot
else
  rm -f /tmp/gpio/ledcontrol/secure_boot
fi
ulimit -c 49152
if runnable gpio-mailbox; then
  reboot-if-fail gpio-mailbox 2>&1 | logos gpio-mailbox &
else
  # gpio-mailbox unavailable, but pretend it is so apps won't wait for it
  : >/var/run/gpio-mailbox
fi

# Move USB interrupt to core 2.
xhci_irq=$(grep xhci-hcd /proc/interrupts | sed -e s/" \([0-9]\+\):.*/\1/")
echo 2 >/proc/irq/${xhci_irq}/smp_affinity

echo 8192 > /proc/sys/vm/min_free_kbytes
